<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title></title>
	<link type="text/css" href="css/style.css" rel="stylesheet" />
	<script type="text/javascript" src="js/jquery-1.10.2.js" ></script>
	<script type="text/javascript">
	</script>
</head>
<body>
	<div class="comment" >
	<?php 
		if(isset($erors) && !empty($erors)){
			foreach($erors as $eror){
				echo "<p>".$eror."</p>";
			}
		}
	?>
	<h2>Gửi bình luận</h2>
	<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
		<label>Name:</label>
		<input type="text" name="name" size="30" value="<?php if(isset($_COOKIE['username_thien'])) echo $_COOKIE['username_thien'] ?>" /><br/>
		<label>Email:</label>
		<input type="email" name="email" size="30" value="<?php if(isset($_COOKIE['email_thien'])) echo $_COOKIE['email_thien'] ?>" /><br/>
		<label>Message:</label>
		<textarea rows="7" cols="60" name="message"></textarea><br/>
		<input type="submit" name="ok" value="Send" />
	</form>
	<h2>Bình luận</h2>
	<?php
		include 'db.php';
		// Create table 
		/*
		$sql = "CREATE TABLE dhv_nguyenvanthien_gestbook 
		( 
		id INT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
		username VARCHAR(255), 
		email VARCHAR(255), 
		comment VARCHAR(255) 
		)"; 
		$query_create=mysql_query($sql,$conection); 
		if($query_create){
			echo "tao table thanh cong";
		}
		*/
		
		if(isset($_POST['ok'])){
			$name=$email=$message="";
			$requied=array('name','email','message');
			$erors=array();
			foreach($requied AS $field_name){
				if(!isset($_POST[$field_name]) || empty($_POST[$field_name])){
					$erors[]= "<p style='color:#f60c0c'>Field <strong>{$field_name}</strong> not blank</p>";
				}
			}
			//lay gia tri nhap vao
			$name=mysql_real_escape_string($_POST['name']);
			$email=mysql_real_escape_string($_POST['email']);
			$message=$_POST['message'];
			if($name && $email && $message){
				$sql="INSERT INTO dhv_nguyenvanthien_gestbook(username,email,comment) VALUES('".$name."','".$email."','".$message."')";
				$query=mysql_query($sql);
			}
		}
		//phan trang
		$record=5;
        if(isset($_GET['start'])){
            $start=$_GET['start'];
        }else{
            $start=0;
        }
        if(isset($_GET['page'])){
            $num_page=$_GET['page'];
        }
        else{
            $sql="SELECT * FROM dhv_nguyenvanthien_gestbook";
            $query=mysql_query($sql);
            $total=mysql_num_rows($query);
            $num_page=ceil($total/$record);
        }
		//lay all comment
		
		$sql2="SELECT * FROM dhv_nguyenvanthien_gestbook ORDER BY id DESC LIMIT $start,$record";
		$query2=mysql_query($sql2);
		if($query2){	
			while($row=mysql_fetch_array($query2)){
				echo "<div class='comment-body'>" .
						"<div class='comment-author vcard'>" .
							"<img alt='' src='http://0.gravatar.com/avatar/420e257e082e02cf43e66c4937839f91?s=60&amp;d=&amp;r=G' class='avatar avatar-60 photo' height='60' width='60' />" .               
							"<strong class='fn'>" .$row['username'] . "</strong>" .
							"<span class='comment-meta commentmetadata'>" . /*date("d-m-Y")*/ "</span>".
						"</div>" .
						"<div class='comment-content'>".
							"<p>" . $row['comment']  ."</p>".
						"</div>".
						"<div class='reply'>".
							"<a class='comment-reply-link' href='' onclick=''>Reply</a>".            
						"</div>".						
					"</div>";
			}
		}
		
		
	?>
	<div align=right class="paging">
		<?php
			if($num_page>1){
				$curent=$start/$record+1;
				if($curent!=1){
					$prev=$start-$record;
					echo "<a href=issue6.php?start=$prev&page=$num_page>Prev</a>";
				}
				if($curent>2){
					$begin=$curent-2;
				}else{
					$begin=1;
				}
				if($curent<$num_page-2){
					$end=$curent+2;
				}
				else{
					$end=$num_page;
				}
				for($i=$begin;$i<=$end;$i++){
					if($i!=$curent){
						$list=($i-1)*$record;
						echo "<a href=issue6.php?start=$list&page=$num_page>$i</a>";
					}
					else{
						echo "<span>$i</span>";
					}
				}
				if($curent!=$num_page){
					$next=$start+$record;
					echo "<a href=issue6.php?start=$next&page=$num_page>Next</a>";
				}
			}
		?>
	</div>
		
		
	</div>
</body>
</html>